package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.common.utils.PageUtils;
import com.ljf.common.utils.Query;
import io.renren.modules.sys.dao.StudentDao;
import io.renren.modules.sys.entity.StudentEntity;
import io.renren.modules.sys.entity.TeacherEntity;
import io.renren.modules.sys.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StudentEntity> page = this.page(
                new Query<StudentEntity>().getPage(params),
                new QueryWrapper<StudentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public StudentEntity queryPageById(Map<String, Object> params, Long id) {
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<StudentEntity>().eq("user_id", id);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer getIdByUserId(Long userId) {
        StudentEntity teacherEntity = this.baseMapper.selectOne(new QueryWrapper<StudentEntity>().eq("user_id", userId));
        if (teacherEntity != null)
            return teacherEntity.getId();
        return null;
    }

    @Override
    public void saveOrUpdateByUserId(Long userId, String username, String mobile, String email) {
        StudentEntity teacherEntity = new StudentEntity();
        teacherEntity.setUserId(userId);
        teacherEntity.setEmail(email);
        teacherEntity.setPhoneNumber(mobile);
        teacherEntity.setRealName(username);
        StudentEntity teacher = this.getBaseMapper().selectOne(new QueryWrapper<StudentEntity>().
                eq("user_id", userId));
        if (teacher != null) {
            teacherEntity.setId(teacher.getId());
            this.updateById(teacherEntity);
        } else {
            this.save(teacherEntity);
        }
    }
}